﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.Common.Helper
{
    public class Base64Helper
    {
        /// <summary>
        /// Base64加密 （utf-8 编码方式）
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的密文</returns>
        public string Base64Encode(string source)
        {
            return Base64Encode(Encoding.UTF8, source);
        }
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encodeType">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的密文</returns>
        public string Base64Encode(Encoding encodeType,string source)
        {
            string encode = string.Empty;
            try
            {
                byte[] bytes = encodeType.GetBytes(source);
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }
        /// <summary>
        /// Base64解密 （utf-8 编码方式）
        /// </summary>
        /// <param name="resule">待解密的密文</param>
        /// <returns>解密后的明文</returns>
        public string Base64Decode(string resule)
        {
            return Base64Decode(Encoding.UTF8, resule);
        }
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="encodeType">解密采用的编码方式，和加密时一致</param>
        /// <param name="resule">待解密的密文</param>
        /// <returns>解密后的明文</returns>
        public string Base64Decode(Encoding encodeType,string resule)
        {
            string decode = string.Empty;
            try
            {
                byte[] bytes = Convert.FromBase64String(resule);
                decode = encodeType.GetString(bytes);
            }
            catch
            {
                decode = resule;
            }
            return decode;
        }
    }
}
